package com.ruoyi.system.service.impl;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.ParkRate;
import com.ruoyi.system.domain.SysHolidayDetail;
import com.ruoyi.system.domain.SysHolidayTotal;
import com.ruoyi.system.mapper.SysHolidayDetailMapper;
import com.ruoyi.system.service.ISysHolidayDetailService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 参数管理模块
 * @author ytl
 */
@Service
public class SysHolidayDetailServiceImpl implements ISysHolidayDetailService {

    @Autowired
    public SysHolidayDetailMapper sysHolidayDetailMapper;

    private static final Logger log = LoggerFactory.getLogger(SysHolidayDetailServiceImpl.class);


    @Override
    public String importSysHolidayDetail(List<SysHolidayDetail> sysHolidayDetailList, Boolean isUpdateSupport, String operName) {
        SysHolidayDetail p;
        if (StringUtils.isNull(sysHolidayDetailList) || sysHolidayDetailList.size() == 0)
        {
            throw new BusinessException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        List<SysHolidayDetail> detailLists = sysHolidayDetailMapper.listSysHolidayDetailAll();
        List<String> list = new ArrayList<>();
        for (SysHolidayDetail sysHolidayDetail : sysHolidayDetailList)
        {
            try
            {
                // 验证是否有UUID
                sysHolidayDetail.setCreateBy(operName);
                if ("".equals(sysHolidayDetail.getHolidayUuid())){
                    sysHolidayDetail.setHolidayUuid(UUID.randomUUID().toString());
                    sysHolidayDetailMapper.insertSysHolidayDetail(sysHolidayDetail);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、项目 " + sysHolidayDetail.getProjectName()
                                +"、姓名"+sysHolidayDetail.getUserName()
                                +"、年假时长"+sysHolidayDetail.getYearHolDate()
                                +"、事假时长"+sysHolidayDetail.getCasuHolDate()
                                +"、病假时长"+sysHolidayDetail.getSickHolDate()+ " 导入成功");
                }else{
                    sysHolidayDetail.setUpdateBy(operName);
                    for (SysHolidayDetail detailList:detailLists){
                        list.add(detailList.getHolidayUuid());
                    }
                    if (list.contains(sysHolidayDetail.getHolidayUuid())){
                        sysHolidayDetailMapper.updateSysHolidayDetail(sysHolidayDetail);
                        successMsg.append("<br/>" + successNum + "、项目 " + sysHolidayDetail.getProjectName()
                                +"、姓名"+sysHolidayDetail.getUserName()
                                +"、年假时长"+sysHolidayDetail.getYearHolDate()
                                +"、事假时长"+sysHolidayDetail.getCasuHolDate()
                                +"、病假时长"+sysHolidayDetail.getSickHolDate()+ " 导入成功");
                    }else{
                        sysHolidayDetailMapper.insertSysHolidayDetail(sysHolidayDetail);
                        successMsg.append("<br/>" + successNum + "、项目 " + sysHolidayDetail.getProjectName()
                                +"、姓名"+sysHolidayDetail.getUserName()
                                +"、年假时长"+sysHolidayDetail.getYearHolDate()
                                +"、事假时长"+sysHolidayDetail.getCasuHolDate()
                                +"、病假时长"+sysHolidayDetail.getSickHolDate()+ " 导入成功");
                    }



                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + successNum + "、项目 " + sysHolidayDetail.getProjectName()
                        +"、姓名"+sysHolidayDetail.getUserName()
                        +"、年假时长"+sysHolidayDetail.getYearHolDate()
                        +"、事假时长"+sysHolidayDetail.getCasuHolDate()
                        +"、病假时长"+sysHolidayDetail.getSickHolDate()+ " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<SysHolidayDetail> selectHolidayDetail(String projectName, String userName) {
        return sysHolidayDetailMapper.selectHolidayDetail(projectName,userName);
    }

    @Override
    public String validateDatas(List<SysHolidayDetail> holidayList) {
        StringBuilder sb =new StringBuilder();
        if (StringUtils.isEmpty(holidayList)){
            sb.append("execel中无任何数据，请认真填写！");
        }else {
            for (int i = 0; i <holidayList.size() ; i++) {
                if (StringUtils.isBlank(holidayList.get(i).getProjectName())){
                    sb.append("第"+(i+1)+"行"+"、项目名称为空！<br/>");
                }
                if (StringUtils.isBlank(holidayList.get(i).getUserName())){
                    sb.append("第"+(i+1)+"行"+"、员工名称为空！<br/>");
                }
                if (StringUtils.isBlank(holidayList.get(i).getYear())){
                    sb.append("第"+(i+1)+"行"+"、年份为空！<br/>");
                }
                if (StringUtils.isBlank(holidayList.get(i).getYearHolTotal())){
                    sb.append("第"+(i+1)+"行"+"、年假总时长为空！<br/>");
                }
                if (StringUtils.isBlank(holidayList.get(i).getHolidayDate())){
                    sb.append("第"+(i+1)+"行"+"、休假日期为空！<br/>");
                }
                if (!holidayList.get(i).getHolidayDate().substring(0,4).equalsIgnoreCase(holidayList.get(i).getYear())){
                    sb.append("第"+(i+1)+"行"+"、年份和休假日期不符！<br/>");
                }
//                if (StringUtils.isBlank(holidayList.get(i).getYearHolDate())){
//                    sb.append("第"+(i+1)+"行"+"、年假时长为空！<br/>");
//                }
//                if (StringUtils.isBlank(holidayList.get(i).getCasuHolDate())){
//                    sb.append("第"+(i+1)+"行"+"、事假时长为空！<br/>");
//                }
//                if (StringUtils.isBlank(holidayList.get(i).getSickHolDate())){
//                    sb.append("第"+(i+1)+"行"+"、病假时长为空！<br/>");
//                }
            }
        }
        if (sb.length()>0){
            return sb.toString();
        };
        return "";
    }

    @Override
    public List<SysHolidayDetail> listSysHolidayDetail(SysHolidayDetail sysHolidayDetail) {
        return sysHolidayDetailMapper.listSysHolidayDetail(sysHolidayDetail);
    }

    @Override
    public int deleteHolidayDetailByIds(String[] ids) {
        return sysHolidayDetailMapper.deleteHolidayDetailByIds(ids);
    }

    @Override
    public int insertSysHolidayDetail(SysHolidayDetail sysHolidayDetail,String operName) {
        sysHolidayDetail.setCreateBy(operName);
        sysHolidayDetail.setHolidayUuid(UUID.randomUUID().toString());
        return sysHolidayDetailMapper.insertSysHolidayDetail(sysHolidayDetail);
    }

    @Override
    public int updateSysHolidayDetail(SysHolidayDetail sysHolidayDetail,String operName) {
        sysHolidayDetail.setUpdateBy(operName);
        return sysHolidayDetailMapper.updateSysHolidayDetail(sysHolidayDetail);
    }

    @Override
    public SysHolidayDetail selectSysHolidayDetail(String holidayUuid) {
        return sysHolidayDetailMapper.selectSysHolidayDetail(holidayUuid);
    }

    @Override
    public List<SysHolidayDetail> listSysHolidayDetailAll() {
        return sysHolidayDetailMapper.listSysHolidayDetailAll();
    }

}
